import Cookies from 'js-cookie'
// import { getVersionInfo } from '@a/common.js'
// import img from '@/assets/images/police-badge.png'

const state = {
//   logo: img,
  title: '',
  version: '1.0.0',
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium'
}

const mutations = {
  SET_TITLE: (state, title) => {
    state.title = title
  },
  //   SET_LOGO: (state, logo) => {
  //     state.logo = logo
  //   },
  SET_VERSION: (state, version) => {
    state.version = version
  },
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  OPEN_SIDEBAR: state => {
    state.sidebar.opened = true
    Cookies.set('sidebarStatus', 1)
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_LANGUAGE: (state, language) => {
    state.language = language
    Cookies.set('language', language)
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  }
}

const actions = {
  setLanguage({ commit }, language) {
    commit('SET_LANGUAGE', language)
  },
  openSideBar({ commit }) {
    commit('OPEN_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  }
//   setTitleAndLogo({ commit }) {
//     return new Promise((resolve, reject) => {
//       getVersionInfo().then(res => {
//         if (res?.code === 0) {
//           commit('SET_TITLE', res?.result?.softName ?? state.title)
//           commit('SET_LOGO', res?.result?.logo ?? state.logo)
//           commit('SET_VERSION', res?.result?.newVersion ?? state.version)
//           document.title = state.title
//           resolve()
//         } else {
//           reject(res.msg)
//         }
//       })
//     })
//   }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
